package com.good.child.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.config.Constants;
import com.good.child.iview.LoginIView;
import com.good.child.pressent.LoginPresenter;
import com.good.child.ui.TestActivity;
import com.good.child.ui.utils.DimensionUtil;
import com.good.child.widgets.editext.ClearEditText;
import com.good.child.widgets.editext.TextWatchAdapter;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.LibraryApplication;
import com.yimidida.library.tool.TActivityUtils;
import com.yimidida.library.utils.LogUtils;
import com.yimidida.library.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Create on: 2024/2/24
 * by Mr.Zhu
 * Description: TODO
 */
public class LoginActivity extends BaseActivity<LoginIView, LoginPresenter> implements LoginIView {
    @BindView(R.id.shop_ed)
    ClearEditText shopEd;
    @BindView(R.id.submit_btn)
    Button submitBtn;
    @BindView(R.id.verify_code_tv)//获取验证码
    TextView verifyCodeTv;

    @BindView(R.id.verify_code_ed)//验证码ed
    ClearEditText verifyCodeEd;

    @BindView(R.id.phone_tips)
    TextView tipsTv;//手机号提示

    private boolean showPass = false;
    public int[] punishTime = {60000, 120000, 180000, 240000, 300000};
    private TimeCount time;
    private int systemCode = 0;

    @Override
    protected LoginPresenter initPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        setStatusBar(true, false);
        setAndroidNativeLightStatusBar(this, true);
//        setTitle("登陆");
//        setRightTvText("绑定", new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                TActivityUtils.jumpToActivity(LoginActivity.this, RegisterActivity.class);
//                TActivityUtils.jumpToActivity(LoginActivity.this, TestActivity.class);
//            }
//        });
        shopEd.addTextChangedListener(new TextWatchAdapter(){
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if (s == null) {
                    return;
                }
                if (s.toString().matches(Constants.PHONE_REGEX)) {
                    tipsTv.setVisibility(View.GONE);
                }
            }
        });
    }


    @Override
    public void onUserLoginSuccess() {
//        TActivityUtils.jumpToActivity(this, TestActivity.class);
        boolean first = (boolean) LibraryApplication.getSharedata().getData(Constants.FIRST_LOGIN, true);
        if (first) {
            TActivityUtils.jumpToActivity(this, SettingInformationActivity.class);
        } else {
            TActivityUtils.jumpToActivity(this, MainActivity.class);
        }
        finish();
    }


    @OnClick({R.id.submit_btn,R.id.verify_code_tv})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.submit_btn:
                onSubmit();
                break;
            case R.id.verify_code_tv:
                startTime();
                break;
        }
    }

    private void startTime() {
        String shopId = shopEd.getText().toString();
        if (TextUtils.isEmpty(shopId)) {
            ToastUtil.showShort("请输入正确的手机号");
            tipsTv.setVisibility(View.VISIBLE);
            return;
        }
        if (!shopId.matches(Constants.PHONE_REGEX)) {
            ToastUtil.showShort("请输入正确的手机号");
            tipsTv.setVisibility(View.VISIBLE);
            return;
        }
        getVerifyCode();
        if (time == null) {
            time = new TimeCount(punishTime[0], 1000);// 构造CountDownTimer对象
        } else {
            time.cancel();
        }
        time.start();
    }

    private void onSubmit() {
        String shopId = shopEd.getText().toString();
        if (TextUtils.isEmpty(shopId)) {
            ToastUtil.showLong(getString(R.string.login_tips_shop));
            return;
        }
        String phone = verifyCodeEd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showLong(getString(R.string.login_tips_phone));
            return;
        }
        mPresenter.getLogin(shopId,"1111");

//        Intent intentMain = new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intentMain);
//        finish();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (time != null) {
            time.cancel();
            time = null;
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        public void setTime(long millisInFuture, long countDownInterval) {
            setTime(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            verifyCodeTv.setText("获取");
            verifyCodeTv.setTextColor(getResources().getColor(R.color.color_FD6900));
            verifyCodeTv.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            verifyCodeTv.setTextColor(getResources().getColor(R.color.color_FF0E2E));
            verifyCodeTv.setText(String.valueOf(millisUntilFinished / 1000)
                    .toString() + "S");
            verifyCodeTv.setEnabled(false);
            verifyCodeTv.postDelayed(() -> {
                systemCode = (int) ((Math.random() * 9 + 1) * 100000);
                if (TextUtils.isEmpty(verifyCodeEd.getText())) {
                    verifyCodeEd.setText(systemCode + "");
                }
            }, 1000);
        }
    }

    /**
     * 获取验证码
     */
    private void getVerifyCode() {
        String shopId = shopEd.getText().toString();
//        verifyCodeEd.setText("33333");
        mPresenter.getVerifyCode(shopId);
    }
}

